<?php

/******************************
Copyright (c) 2009, Avanade
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

* Neither the name of Avanade nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
******************************/

require_once 'facebook-platform/php/facebook.php';

  // NOTE: adjust the paths of the CSS and image files here according to your web site structure. The CSS file styles the output 
  // web slice
   
  $cssFile = "fbStatusStyles.css";
  
  // Image for users with no stored profile picture
  $noImage = "NoImage.gif";
  
  // The API key and secret number are generated when you create your facebook developer application
  $facebook = new Facebook('your API Key here','your secret number here');

/* require the user to be logged into Facebook before
  using the application. If they are not logged in they
  will first be directed to a Facebook login page and then
  back to the application's page. require_login() returns
  the user's unique ID which we will store in fb_user */
		$fb_user = $facebook->require_login();
   
	// The API calls may take than the default 30 seconds time period. So setting infinite time limit
		set_time_limit(0);
	
	// friends_get() gets the list of friends for the logged in user
		$friends = $facebook->api_client->friends_get();
		
	// users_getInfo() gets the name and status of all  friends
		$user_details=$facebook->api_client->users_getInfo($friends , array('name,status,pic'));
  
	$count = count($user_details);

	$IsUpdate =0;
  
  // Start preparing the page to format the output
  echo '<html><head><title>Facebook Friends Status</title>';
  
  // Create link to stylesheet
  echo '<link rel="Stylesheet" type="text/css" href="'.$cssFile.'" /></head><body>';
  
  // Create the div containing the web slice
  // NOTE: this div must contain an id and a class=hslice for IE to recognize the web slice
  echo '<div id="statusupdates" class="hslice">';
  
  // Add the web slice title
  echo '<h2 class="entry-title">Facebook Status Updates</h2>';
  
  // Add the paragraph containing the web slice content
  echo '<div class="entry-content" id="wsContent">';
  
  // loop through the $user_details array to print the status message of friends
	for ($counter = 0; $counter <= $count; $counter ++) {
		
  // Get the current time and status update time. Then find the time difference.
		  $StatusinSeconds = $user_details[$counter]['status']['time'];
		  $Currenttime= time();
		 
		  $DiffinSeconds= $Currenttime-$StatusinSeconds;
		  $DiffinMinutes=$DiffinSeconds/60;
		  $DiffinHours= $DiffinSeconds/60/60;

 // Display only if the user  has a status message update in last 7 days
		  
		if ($user_details[$counter]['status']['status_id'] > "0" && $DiffinHours<168)
		{			
	      $IsUpdate = 1;
      
      // Create a list item containing a div for each entry to contain image and status text
      echo '<div id="statusItem">';
      
 // Get the picture andto display it. If picture not available then show the default image

      // Create span which holds image and defines styling
      echo '<div id="friendImage">';
		  if($user_details[$counter]['pic']!= '')
		  {
			  
		 	echo '<img src="'.$user_details[$counter]['pic'].'" />';
		  }
		  else
		  {
			  echo '<img src="'.$noImage.'" />';
		  }
      // Close image span
      echo '</div>';
      
      
      // Create a container to hold paragraph for friend's name and status text
      echo '<div id="friendStatus"><p><span class="friendName">'.$user_details[$counter]['name'].'</span>'.' '.$user_details[$counter]['status']['message'].'<br />';
		   
  // Show the update time in days, hours or minutes
		  if( $DiffinHours>=24)
		  {
			  // divide by 24 to determine how many days ago the update was made, round down to truncate any decimals
			  $daysAgo = floor($DiffinHours/24);
			  // subtract number of days from todays date to get when the status message was updated
			  echo ' on ' .date('l', strtotime(-$daysAgo . 'days'));
		  }
		  else if ($DiffinHours>1)
		  {
			  echo  round((int) $DiffinHours).' hours ago';
		  }

		  else if ($DiffinMinutes>1)
		  {
			  echo round((int) $DiffinMinutes).' minutes ago';
		  }
		  else if ($DiffinMinutes=1)
		  {
			  echo round((int) $DiffinMinutes).' minute ago'; 	
		  }
      
        // Close para wrapping the update time, div and list item containing the current friend
      echo '</p></div></div>';
		}
	
	}

  // If there are no status  updates then pass that info to the user
	If($IsUpdate==0)
	{
    echo '<p>';
		echo 'There are no new status updates';
    echo '</p>';
	}
  
  // close the remaining tags
  echo '</div></div></body></html>';
 ?>